<template>
	<view>
		<u-navbar backIconColor="#ffffff" titleColor="#ffffff" :title="title" :background="{background: 'var(--mainColor)'}">
		</u-navbar>
		<view class="u-p-x-40 bg-white">
			<u-form :model="form" ref="uForm" label-position="left">
				<view class="">
					<u-form-item prop="mobile">
						<u-input v-model="form.mobile" type="number" placeholder="请输入手机号码" />
					</u-form-item>
					<u-form-item prop="captcha">
						<view class="flex flex-1">
							<u-input class="flex-1" v-model="form.captcha" type="number" maxlength="6" placeholder="请输入验证码" />
							<dy-phone-code-button :mobile='form.mobile' event="retrieve"></dy-phone-code-button>
						</view>
					</u-form-item>
				</view>
				<view class="" v-if="type == 2">
					<u-form-item prop="newpassword">
						<u-input v-model="form.newpassword" type="password" placeholder="请确认新的密码" />
					</u-form-item>
					<u-form-item prop="renewpass">
						<u-input v-model="form.renewpass" type="password" placeholder="请确认新的密码" />
					</u-form-item>
				</view>
				<view class="" v-if="type == 3">
					<u-form-item prop="newpaypwd">
						<u-input v-model="form.newpaypwd" type="password" placeholder="请确认新的交易密码" />
					</u-form-item>
					<u-form-item prop="newpaypwd2">
						<u-input v-model="form.newpaypwd2" type="password" placeholder="请确认新的交易密码" />
					</u-form-item>
				</view>
			</u-form>
		</view>
		<view class="" style="margin-top: 200rpx;">
      <dy-button width="576rpx" size="default" @click="sure">保存</dy-button>
		</view>
	</view>
</template>

<script>
	import { BaseCenterApi } from '@/api/serves.js'
	export default {
		data() {
			return {
				title: '',
				type: 1,
				showPopup: true,
				form: {
					mobile: '',
					captcha: '',

					newpassword: '',
					renewpass: '',

					newpaypwd: '',
					newpaypwd2: '',
				},
				rules: {
					mobile: [{
							required: true,
							message: '请输入手机号码',
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['blur'],
						},
						{
							validator: (rule, value, callback) => {
								if (!this.$u.test.mobile(value)) {
									callback(new Error("请输入正确的手机号码"));
								} else {
									callback();
								}
							},
						},
					],
					captcha: [{
						len: 6,
						required: true,
						message: '请输入6位数验证码',
						trigger: ['blur', 'change'],
					}],
					newpassword: [{
						min: 6,
						max: 20,
						required: true,
						message: '密码长度在6到15之间',
						trigger: ['blur', 'change'],
					}],
					renewpass: [{
						min: 6,
						max: 20,
						required: true,
						message: '密码长度在6到15之间',
						trigger: ['blur', 'change'],
					}, {
						validator: (rule, value, callback) => {
							if (value == this.form.newpassword) {
								callback();
							} else {
								callback(new Error("密码不一致"));
							}
						},
					}],
					newpaypwd: [{
						required: true,
						message: '交易密码长度为6',
						trigger: ['blur', 'change'],
					}],
					newpaypwd2: [{
						required: true,
						message: '交易密码长度为6',
						trigger: ['blur', 'change'],
					}, {
						validator: (rule, value, callback) => {
							if (value == this.form.newpaypwd) {
								callback();
							} else {
								callback(new Error("密码不一致"));
							}
						},
					}],
				}
			}
		},
		onLoad(options) {
			let obj = {
				1: '修改手机号码',
				2: '修改登录密码',
				3: '修改支付密码'
			}
			this.type = options.type || 1
			this.title = obj[options.type]
		},
		mounted() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			sure() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						if(this.type == 1) return this._changemobile()
						if(this.type == 2) return this._uppass()
						if(this.type == 3) return this._uppaypsw()
					}
				});
			},
			_changemobile() {
				BaseCenterApi.changemobile(this.form).then(res=> {
					console.log('修改手机号', res);
					if(res.code == 1) {
						uni.navigateBack()
					}else {
						this.$u.toast(res.msg)
					}
				})
			},
			_uppass() {
				BaseCenterApi.uppass(this.form).then(res=> {
					console.log('修改登录密码', res);
					if(res.code == 1) {
						this.$Toast('修改成功')
						setTimeout(()=> {
							uni.navigateBack()
						}, 1000)
					}else {
						this.$u.toast(res.msg)
					}
				})
			},
			_uppaypsw() {
				BaseCenterApi.uppaypsw(this.form).then(res=> {
					console.log('修改支付密码', res);
					if(res.code == 1) {
						this.$Toast('修改成功')
						setTimeout(()=> {
							uni.navigateBack()
						}, 1000)
					}else {
						this.$u.toast(res.msg)
					}
				})
			},
		}
	}
</script>

<style scoped lang="scss">
::v-deep.u-form-item__message {
	padding-left: 0 !important;
}
</style>
